package com.mars.weixin.web.boss.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.web.ResponseResult;
import com.mars.base.web.BaseController;
import com.mars.base.web.model.DataGrid;
import com.mars.boss.weixin.entity.WxAgRelevancy;
import com.mars.boss.weixin.entity.WxArticle;
import com.mars.boss.weixin.entity.WxArticleGroup;
import com.mars.boss.weixin.service.WxArticleGroupService;

@RestController
@RequestMapping("/weixin/article_group")
public class WxArticleGroupController extends BaseController {

	@Autowired
	private WxArticleGroupService service;

	@PostMapping(value = "/add")
	public Object add(WxArticleGroup entity, Long[] articleIds) {
		try {
			this.service.insertArticleGroup(entity, articleIds);
		} catch (AppException e) {
			return ResponseResult.addFailed();
		}
		return ResponseResult.addOk();
	}

	@GetMapping(value = "/delete/{ids}")
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids);
		} catch (AppException e) {
			return ResponseResult.deleteFailed();
		}
		return ResponseResult.deleteOk();
	}

	@PostMapping(value = "/update")
	public Object update(WxArticleGroup entity, Long[] articleIds) {
		try {
			this.service.updateArticleGroup(entity, articleIds);
		} catch (AppException e) {
			return ResponseResult.updateFailed();
		}
		return ResponseResult.updateOk();
	}

	@GetMapping(value = "/get/{id}")
	public WxArticleGroup get(@PathVariable Long id) {
		return this.service.getArticleGroupAndArticles(id) ;
	}

	@GetMapping(value = "/list")
	public Object list(WxArticleGroup entity) {
		try {
			PageInfo<WxArticleGroup> selectPage = this.service.selectPage(getPageParam(), entity);
			DataGrid<WxArticleGroup> dg = new DataGrid<WxArticleGroup>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			return dg ;
		} catch (AppException e) {
			return ResponseResult.loadFailed();
		}
	}
	
	@GetMapping(value = "/combobox")
	public Object combobox(WxArticleGroup entity) {
		return this.service.combobox() ;
	}
	
	
	@PostMapping(value = "/addArticle")
	public Object addArticle(Long articleGroupId, Long[] articleIds, Integer[] orders) {
		try {
			//清空该多图文下的图文
			this.service.deleteByArticleGroupId(articleGroupId) ;
			
			
			List<WxAgRelevancy> list = new ArrayList<WxAgRelevancy>() ;
			for(int i=0; i<articleIds.length; i++) {
				list.add(new WxAgRelevancy(articleGroupId, articleIds[i], orders[i])) ;
			}
			//添加图文到图文组
			this.service.addArticleList(list) ;
			
		} catch (AppException e) {
			return ResponseResult.ok();
		}
		return ResponseResult.failure();
	}
	
	@GetMapping(value = "/getArticleList/{articleGroupId}")
	public List<WxArticle> getArticleList(@PathVariable Long articleGroupId) {
		return this.service.getArticles(articleGroupId);
	}

}
